<?php

class Admin_AuthController extends Ext_Controller_Action_Admin
{
    public function init()
    {
        parent::init();
    }
    
    public function indexAction()
    {
         //$this->_forward('someaction', 'somecontroller');
    }
    
    public function logoutAction()
    {
        Ext_Auth_Admin::getInstance()->clear();
        
        $viewResult = new Ext_View_Result();
        $viewResult->addMessage($this->translater->_('Logout success.'));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function saveLoginPostAction()
    {
        $viewResult = new Ext_View_Result();
        
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        if(!$userId){
            $this->_forward("show-please-login", "auth", "account");
        }
        
        $password = $this->filter->filter($this->getRequest()->getPost('password'));
        $captcha  = $this->getRequest()->getPost('captcha');
        
        $validater = new Ext_Validate();
        $validater->addValidater(new Add_Admin_Auth_SaveLoginPost_Validate_Password($password));
        $validater->addValidater(new Ext_Validate_Adapter_Captcha($captcha));
        if(!$validater->isValid()){
            $viewResult->addMessages($validater->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $data = new Ext_User_Verify_Data();
        $data->setUserId($userId);
        $data->setPassword($password);
        $verify = new Ext_User_Verify($data);
        $verifyResult = $verify->verify();
        if(!$verifyResult->isValid()){
            $viewResult->addMessages($verifyResult->getMessages());
            $viewResult->setNoRender();
            return "";
        }
        
        $auth = Ext_Auth_Admin::getInstance();
        $auth->auth($userId);
        
        $viewResult->addMessage($this->translater->_('Login success.'));
        $viewResult->setResult(1);
        $viewResult->setNoRender();
    }
    
    public function showLoginFormAction()
    {
        $userId = Ext_Auth_Front::getInstance()->getIdentity();
        if(!$userId){
            $this->_forward("show-please-login", "auth", "account");
        }
        
        $viewForm = new Ext_View_Form_Admin_Auth_Login();
        
        $user = Ext_User::getInstance()->show($userId);
        if(isset($user->nickname)){
            $viewForm->getElement("nickname")->setValue($user->nickname);
        }
        
        $this->_helper->getHelper('Layout')->setLayout('module_admin_layout-auth');
        $this->view->form = $viewForm;
    }
    
    public function showAccessDeniedAction()
    {
        /*
        $viewResult = new Ext_View_Result();
        
        $translate = Zend_Registry::get('Zend_Translate');

        $viewResult->setMessage("Access denied.");

        $viewResult->setResult(0);
        $viewResult->setNoRender();
       */
    }
}

